package com.fivefu.core.data.services.impl;

import com.fivefu.core.data.entity.*;
import com.fivefu.core.data.services.*;
import com.fivefu.core.data.vo.TreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class FfskyeyeTreeServiceImpl implements FfskyeyeTreeService {

    @Autowired
    private DbFfskyeyeRegionService dbFfskyeyeRegionService;
    @Autowired
    private DbFfskyeyeDeviceService dbFfskyeyeDeviceService;
    @Autowired
    private DbFfskyeyeChannelService dbFfskyeyeChannelService;
    @Autowired
    private DbFfskyeyeRegionDeviceService dbFfskyeyeRegionDeviceService;
    @Autowired
    private DbFfskyeyePlatformService dbFfskyeyePlatformService;

    /**
     * 查询树形结构列表
     * @return
     */
    @Override
    public List<TreeVo> queryTreeList(){
        //域列表
        List<DbFfskyeyeRegion> regionList = dbFfskyeyeRegionService.queryPlatFormList(null);
        Map<String, List<DbFfskyeyeRegion>> regionMap = regionList.stream().collect(Collectors.groupingBy(k -> k.getPlatformCode(), Collectors.toList()));

        //设备列表
        List<DbFfskyeyeDevice> deviceList = dbFfskyeyeDeviceService.queryDevicelList(0, null);
        Map<String, List<DbFfskyeyeDevice>> deviceMap = deviceList.stream().collect(Collectors.groupingBy(k -> k.getPlatformCode(), Collectors.toList()));

        //通道列表
        List<DbFfskyeyeChannel> channelList = dbFfskyeyeChannelService.queryChannelList(0, null);
        Map<String, List<DbFfskyeyeChannel>> channelMap = channelList.stream().collect(Collectors.groupingBy(k -> k.getPlatformCode(), Collectors.toList()));

        //域设备关联信息
        List<DbFfskyeyeRegionDevice> regionDeviceList = dbFfskyeyeRegionDeviceService.queryRegionDeviceList(null);
        Map<Long, List<Long>> regionDeviceMap = regionDeviceList.stream().collect(Collectors.groupingBy(k -> k.getRegionId(), Collectors.mapping(v -> v.getDeviceId(), Collectors.toList())));

        List<TreeVo> treelist = new ArrayList<>();
        List<DbFfskyeyePlatform> list = dbFfskyeyePlatformService.queryPlatformList();
        list.forEach(platform -> {
            List<TreeVo> treelist1 = queryTreeList(platform , regionMap, deviceMap, channelMap, regionDeviceMap);
            treelist.addAll(treelist1);
        });


        /*List<TreeVo> treelist = new ArrayList<>();
        List<TreeVo> treelist2 = new ArrayList<>();
        if (regionMap.containsKey("海康")) {
            treelist = queryTreeList("海康", regionMap, deviceMap, channelMap, regionDeviceMap);
        }
        if (regionMap.containsKey("大华")) {
            treelist2 = queryTreeList("大华", regionMap, deviceMap, channelMap, regionDeviceMap);
        }
        treelist.addAll(treelist2);*/
        return treelist;
    }

    /**
     * 获取树形结构
     * @param platform 平台信息
     * @param regionMap 域信息
     * @param deviceMap 设备信息
     * @param channelMap 通道信息
     * @param regionDeviceMap 域设备关联信息
     * @return
     */
    public List<TreeVo> queryTreeList(DbFfskyeyePlatform platform, Map<String, List<DbFfskyeyeRegion>> regionMap, Map<String, List<DbFfskyeyeDevice>> deviceMap, Map<String, List<DbFfskyeyeChannel>> channelMap, Map<Long, List<Long>> regionDeviceMap){
        String type = platform.getPlatformCode();
        String treeNode = platform.getNode();
        Boolean enable = (platform.getEnable() != null && platform.getEnable().equals(1)) ? true : false;

        List<DbFfskyeyeRegion> regions = regionMap.get(type);

        Map<Long, DbFfskyeyeDevice> devicesMap = new HashMap<>();
        Set<Long> deviceSet = new HashSet<>();
        if (deviceMap.containsKey(type)) {
            List<DbFfskyeyeDevice> devices = deviceMap.get(type);
            devicesMap = devices.stream().collect(Collectors.toMap(k -> k.getId(), v -> v));
            deviceSet = devices.stream().map(k -> k.getId()).collect(Collectors.toSet());
        }

        Map<Long, List<DbFfskyeyeChannel>> deviceChannelMap = new HashMap<>();
        Map<Long, List<DbFfskyeyeChannel>> regionChannelMap = new HashMap<>();
        if (channelMap.containsKey(type)){
            List<DbFfskyeyeChannel> channels = channelMap.get(type);
            deviceChannelMap = channels.stream().filter(k -> k.getDeviceId() != null).collect(Collectors.groupingBy(k -> k.getDeviceId(), Collectors.toList()));
            regionChannelMap = channels.stream().filter(k -> k.getRegionId() != null && k.getDeviceId() == null).collect(Collectors.groupingBy(k -> k.getRegionId(), Collectors.toList()));
        }

        List<TreeVo> list = new ArrayList<>();

        TreeVo treeVo = new TreeVo();
        String parentNode = "region_" + treeNode;
        treeVo.setCode(type);
        /*String parentNode = "";
        TreeVo treeVo = new TreeVo();
        if (type.equals("海康")) {
            parentNode = "region_00";
            treeVo.setCode("hikvision");
        }else{
            parentNode = "region_01";
            treeVo.setCode("hillstone");
        }*/
        treeVo.setId(parentNode);
        treeVo.setName(platform.getPlatformName());
        treeVo.setPid("0");
        treeVo.setType("platform");
        treeVo.setEnable(enable);
        list.add(treeVo);
        if (regions != null && regions.size() > 0) {
            for (DbFfskyeyeRegion region : regions) {
                Long regionID = region.getId();
                TreeVo regionTree = new TreeVo();
                regionTree.setId("region_" + regionID);
                regionTree.setNodeId(regionID + "");
                regionTree.setCode(region.getCode());
                if (region.getParentid() == null || region.getParentid().equals(-1)) {
                    regionTree.setPid(parentNode);
                    regionTree.setParentId("0");
                } else {
                    regionTree.setPid("region_" + region.getParentid());
                    regionTree.setParentId(region.getParentid() + "");
                }
                regionTree.setName(region.getName());
                regionTree.setType("region");
                regionTree.setEnable(enable);
                list.add(regionTree);

                if (regionDeviceMap.containsKey(regionID)) {
                    List<Long> deviceList = regionDeviceMap.get(regionID);

                    for (Long deviceID : deviceList) {
                        if (devicesMap.containsKey(deviceID)) {
                            deviceSet.remove(deviceID);
                            DbFfskyeyeDevice device = devicesMap.get(deviceID);

                            TreeVo deviceTree = new TreeVo();
                            deviceTree.setId("device_" + device.getId());
                            deviceTree.setNodeId(device.getId() + "");
                            deviceTree.setCode(device.getDeviceCode());
                            deviceTree.setPid("region_" + regionID);
                            deviceTree.setParentId(regionID + "");
                            deviceTree.setParentCode(region.getCode() + "");
                            deviceTree.setName(device.getDeviceName());
                            deviceTree.setType("device");
                            deviceTree.setEnable(enable);
                            list.add(deviceTree);

                            if (deviceChannelMap.containsKey(deviceID)) {
                                List<DbFfskyeyeChannel> channelList = deviceChannelMap.get(deviceID);
                                for (DbFfskyeyeChannel channel : channelList) {
                                    TreeVo channelTree = new TreeVo();
                                    channelTree.setId("channel_" + channel.getId());
                                    channelTree.setNodeId(channel.getId() + "");
                                    channelTree.setCode(channel.getChannelCode());
                                    channelTree.setPid("device_" + deviceID);
                                    channelTree.setParentId(deviceID + "");
                                    channelTree.setParentCode(device.getDeviceCode());
                                    channelTree.setName(channel.getChannelName());
                                    channelTree.setType("channel");
                                    channelTree.setEnable(enable);
                                    list.add(channelTree);
                                }
                            }
                        }


                    }
                }
                if (regionChannelMap.containsKey(regionID)) {
                    List<DbFfskyeyeChannel> channelList = regionChannelMap.get(regionID);
                    for (DbFfskyeyeChannel channel : channelList) {
                        TreeVo channelTree = new TreeVo();
                        channelTree.setId("channel_" + channel.getId());
                        channelTree.setNodeId(channel.getId() + "");
                        channelTree.setCode(channel.getChannelCode());
                        channelTree.setPid("region_" + regionID);
                        channelTree.setParentId(regionID + "");
                        channelTree.setParentCode(region.getCode() + "");
                        channelTree.setName(channel.getChannelName());
                        channelTree.setType("channel");
                        channelTree.setEnable(enable);
                        list.add(channelTree);
                    }
                }
            }
        }
        /*if (deviceSet != null && deviceSet.size() > 0){
            for (Long deviceID : deviceSet) {
                if (devicesMap.containsKey(deviceID)){
                    DbFfskyeyeDevice device = devicesMap.get(deviceID);

                    TreeVo deviceTree = new TreeVo();
                    deviceTree.setId("device_" + device.getId());
                    deviceTree.setNodeId(device.getId() + "");
                    deviceTree.setCode(device.getDeviceCode());
                    deviceTree.setPid(parentNode);
                    deviceTree.setParentId("0");
                    deviceTree.setName(device.getDeviceName());
                    deviceTree.setType("device");
                    list.add(deviceTree);

                    if (deviceChannelMap.containsKey(deviceID)){
                        List<DbFfskyeyeChannel> channelList = deviceChannelMap.get(deviceID);
                        for (DbFfskyeyeChannel channel : channelList) {
                            TreeVo channelTree = new TreeVo();
                            channelTree.setId("channel_" + channel.getId());
                            channelTree.setNodeId(channel.getId() + "");
                            channelTree.setCode(channel.getChannelCode());
                            channelTree.setPid("device_" + deviceID);
                            channelTree.setParentId(deviceID + "");
                            channelTree.setParentCode(device.getDeviceCode());
                            channelTree.setName(channel.getChannelName());
                            channelTree.setType("channel");
                            list.add(channelTree);
                        }
                    }
                }
            }
        }*/
        return list;
    }

    /**
     * 递归查询下级结构
     * @param parentId 父级编号
     * @param treeList 树形结构信息
     * @return
     */
    public List<TreeVo> queryTreeListByParentId(String parentId, List<TreeVo> treeList){
        List<TreeVo> list = treeList.stream().filter(k -> k.getPid().equals(parentId)).collect(Collectors.toList());
        for (TreeVo treeVo : list) {
            List<TreeVo> childers = queryTreeListByParentId(treeVo.getId(), treeList);
            treeVo.setChildren(childers);
        }
        return list;
    }

    /**
     * 查询树形结构子父级关系
     * @return
     */
    @Override
    public List<TreeVo> queryTreeChildrenList(){
        List<TreeVo> list = queryTreeList();
        List<TreeVo> treelist = queryTreeListByParentId("0", list);
        return treelist;
    }

}
